﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.Runtime;
using BPM.Foundation.WFDB;
using BPM.Base.Tools;

namespace BPM.Foundation.WFService
{
    public partial interface IWorkflowService
    {
        [System.ServiceModel.OperationContract]
        List<SYS_Log> getSYSLog(string pid, DateTime startTime, DateTime endTime, String level, String type, Guid instanceId);

        [System.ServiceModel.OperationContract]
        int? insertSYSLog(string pid, SYS_Log sysLog);

        [System.ServiceModel.OperationContract]
        int? deleteSYSLog(string pid, DateTime startTime, DateTime endTime, String level, String type, Guid instanceId);
    }
    public partial class WorkflowService : IWorkflowService
    {
        void outMessage(string v)
        {
            System.Console.WriteLine(v);
        }

        //[5.1  得到系统日志]
        public List<SYS_Log> getSYSLog(string pid,DateTime startTime, DateTime endTime, String level, String type, Guid instanceId)
        {
            try
            {
                using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
                {
                    List<SYS_Log> items = null;
                    items = wfDB.SYS_Log.Where(p => (startTime.CompareTo(p.createdTime) <= 0 && endTime.CompareTo(p.createdTime) >= 0)).OrderByDescending(p => p.createdTime).ToList();
                    if (!String.IsNullOrEmpty(level))
                    {
                        items = items.Where(p => p.logLevel.Contains(level)).ToList();
                    }
                    if (!String.IsNullOrEmpty(type))
                    {
                        items = items.Where(p => p.logType.Contains(type)).ToList();
                    }
                    if (instanceId != Guid.Empty)
                    {
                        items = items.Where(p => p.instanceId == instanceId).ToList();
                    }
                    return items;
                }
            }
            catch (Exception ex)
            {
                MyLog.writeError(ex.Message);
                return null;
            }
        }
       
        //[5.2  添加系统日志]
        public int? insertSYSLog(string pid, SYS_Log sysLog)
        {
            using (WFDBDataContext mywfdb = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                //[1]
                if (sysLog == null)
                {
                    return 1;
                }
                //[2]
                if (String.IsNullOrEmpty(sysLog.logType))
                {
                    return 2;
                }
                if (String.IsNullOrEmpty(sysLog.logLevel))
                {
                    sysLog.logLevel = "消息";
                }
                sysLog.createdTime = DateTime.Now;
                sysLog.ID = Guid.NewGuid();
                mywfdb.SYS_Log.InsertOnSubmit(sysLog);
                try
                {
                    mywfdb.SubmitChanges();
                    return null;
                }
                catch (Exception ex)
                {
                    MyLog.writeError(ex.Message);
                    return 1;
                }
            }

        }
       
        //[5.3  清除系统日志]
        public int? deleteSYSLog(string pid, DateTime startTime, DateTime endTime, String level, String type, Guid instanceId)
        {
            using (WFDBDataContext wfDB = new WFDBDataContext(MyConfig.getConnection("myconstring")))
            {
                if (startTime > endTime)
                {
                    return 2;
                }
                if (startTime == null || endTime == null)
                {
                    return 3;
                }
                List<SYS_Log> items = null;

                items = wfDB.SYS_Log.Where(p => (startTime.CompareTo(p.createdTime) <= 0 && endTime.CompareTo(p.createdTime) >= 0) || p.logLevel == level || p.instanceId == instanceId || p.logType == type).ToList();


                foreach (SYS_Log s in items)
                {
                    wfDB.SYS_Log.DeleteOnSubmit(s);
                }
                try
                {
                    wfDB.SubmitChanges();
                    return null;
                }
                catch (Exception ex)
                {
                    MyLog.writeError(ex.Message);
                    return 1;
                }
            }
        }


        void runtime_Stopped(object sender, WorkflowRuntimeEventArgs e)
        {
            SYS_Log _log = new SYS_Log();
            _log.instanceId = Guid.Empty;
            _log.logLevel = "消息";
            _log.logType = "Stopped";
            _log.logContent = runtime.Name;
            insertSYSLog("", _log);

            string s = string.Format("{0}runtime,在{1},发生{2}", runtime.Name, System.DateTime.Now.ToString(), "Stopped");
            outMessage(s);
        }

        void runtime_Started(object sender, WorkflowRuntimeEventArgs e)
        {
            SYS_Log _log = new SYS_Log();
            _log.instanceId = Guid.Empty;
            _log.logLevel = "消息";
            _log.logType = "Started";
            _log.logContent = runtime.Name;
            insertSYSLog("", _log);

            string s = string.Format("{0}runtime,在{1},发生{2}", runtime.Name, System.DateTime.Now.ToString(), "Started");
            outMessage(s);
        }

        void runtime_ServicesExceptionNotHandled(object sender, ServicesExceptionNotHandledEventArgs e)
        {
            SYS_Log _log = new SYS_Log();
            _log.instanceId = e.WorkflowInstanceId;
            _log.logContent = e.Exception.Message;
            _log.logLevel = "消息";
            _log.logType = "ServicesExceptionNotHandled";
            insertSYSLog("", _log);

            string s = string.Format("{0}runtime,在{1},发生{2}", runtime.Name, System.DateTime.Now.ToString(), "ServicesExceptionNotHandled" + e.Exception.Message);
            string v = string.Format("{0},{1},{2},{3}", e.WorkflowInstanceId.ToString(), "ServicesExceptionNotHandled", System.DateTime.Now.ToString(), e.Exception.Message);
            outMessage(v);
        }

        void runtime_WorkflowUnloaded(object sender, WorkflowEventArgs e)
        {
            SYS_Log _log = new SYS_Log();
            _log.instanceId = e.WorkflowInstance.InstanceId;
            _log.logContent = "-";
            _log.logLevel = "消息";
            _log.logType = "WorkflowUnloaded";
            insertSYSLog("", _log);

            string v = string.Format("{0},{1},{2},{3}", e.WorkflowInstance.InstanceId.ToString(), "WorkflowUnloaded", System.DateTime.Now.ToString(), "-");
            outMessage(v);
        }

        void runtime_WorkflowTerminated(object sender, WorkflowTerminatedEventArgs e)
        {
            SYS_Log _log = new SYS_Log();
            _log.instanceId = e.WorkflowInstance.InstanceId;
            _log.logContent = e.Exception.Message;
            _log.logLevel = "消息";
            _log.logType = "WorkflowTerminated";
            insertSYSLog("", _log);

            string v = string.Format("{0},{1},{2},{3}", e.WorkflowInstance.InstanceId.ToString(), "WorkflowTerminated", System.DateTime.Now.ToString(), e.Exception.Message);
            outMessage(v);
        }

        void runtime_WorkflowSuspended(object sender, WorkflowSuspendedEventArgs e)
        {
            SYS_Log _log = new SYS_Log();
            _log.instanceId = e.WorkflowInstance.InstanceId;
            _log.logContent = e.Error;
            _log.logLevel = "消息";
            _log.logType = "WorkflowSuspended";
            insertSYSLog("", _log);

            string v = string.Format("{0},{1},{2},{3}", e.WorkflowInstance.InstanceId.ToString(), "WorkflowSuspended", System.DateTime.Now.ToString(), e.Error);
            outMessage(v);
        }

        void runtime_WorkflowStarted(object sender, WorkflowEventArgs e)
        {
            SYS_Log _log = new SYS_Log();
            _log.instanceId = e.WorkflowInstance.InstanceId;
            _log.logContent = "-";
            _log.logLevel = "消息";
            _log.logType = "WorkflowSuspended";
            insertSYSLog("", _log);

            string v = string.Format("{0},{1},{2},{3}", e.WorkflowInstance.InstanceId.ToString(), "WorkflowStarted", System.DateTime.Now.ToString(), "-");
            outMessage(v);
        }

        void runtime_WorkflowResumed(object sender, WorkflowEventArgs e)
        {
            SYS_Log _log = new SYS_Log();
            _log.instanceId = e.WorkflowInstance.InstanceId;
            _log.logContent = "-";
            _log.logLevel = "消息";
            _log.logType = "WorkflowResumed";
            insertSYSLog("", _log);

            string v = string.Format("{0},{1},{2},{3}", e.WorkflowInstance.InstanceId.ToString(), "WorkflowResumed", System.DateTime.Now.ToString(), "-");
            outMessage(v);
        }

        void runtime_WorkflowPersisted(object sender, WorkflowEventArgs e)
        {
            SYS_Log _log = new SYS_Log();
            _log.instanceId = e.WorkflowInstance.InstanceId;
            _log.logContent = "-";
            _log.logLevel = "消息";
            _log.logType = "WorkflowPersisted";
            insertSYSLog("", _log);

            string v = string.Format("{0},{1},{2},{3}", e.WorkflowInstance.InstanceId.ToString(), "WorkflowPersisted", System.DateTime.Now.ToString(), "-");
            outMessage(v);
        }

        void runtime_WorkflowLoaded(object sender, WorkflowEventArgs e)
        {
            SYS_Log _log = new SYS_Log();
            _log.instanceId = e.WorkflowInstance.InstanceId;
            _log.logContent = "-";
            _log.logLevel = "消息";
            _log.logType = "WorkflowLoaded";
            insertSYSLog("", _log);

            string v = string.Format("{0},{1},{2},{3}", e.WorkflowInstance.InstanceId.ToString(), "WorkflowLoaded", System.DateTime.Now.ToString(), "-");
            outMessage(v);
        }

        void runtime_WorkflowIdled(object sender, WorkflowEventArgs e)
        {
            SYS_Log _log = new SYS_Log();
            _log.instanceId = e.WorkflowInstance.InstanceId;
            _log.logContent = "-";
            _log.logLevel = "消息";
            _log.logType = "WorkflowIdled";
            insertSYSLog("", _log);

            string v = string.Format("{0},{1},{2},{3}", e.WorkflowInstance.InstanceId.ToString(), "WorkflowIdled", System.DateTime.Now.ToString(), "-");
            outMessage(v);
        }

        void runtime_WorkflowCreated(object sender, WorkflowEventArgs e)
        {
            SYS_Log _log = new SYS_Log();
            _log.instanceId = e.WorkflowInstance.InstanceId;
            _log.logContent = "-";
            _log.logLevel = "消息";
            _log.logType = "WorkflowCreated";
            insertSYSLog("", _log);

            string v = string.Format("{0},{1},{2},{3}", e.WorkflowInstance.InstanceId.ToString(), "WorkflowCreated", System.DateTime.Now.ToString(), "-");
            outMessage(v);
        }

        void runtime_WorkflowAborted(object sender, WorkflowEventArgs e)
        {
            SYS_Log _log = new SYS_Log();
            _log.instanceId = e.WorkflowInstance.InstanceId;
            _log.logContent = "-";
            _log.logLevel = "警告";
            _log.logType = "WorkflowAborted";
            insertSYSLog("", _log);

            string v = string.Format("{0},{1},{2},{3}", e.WorkflowInstance.InstanceId.ToString(), "WorkflowAborted", System.DateTime.Now.ToString(), "-");
            outMessage(v);
        }

        void runtime_WorkflowCompleted(object sender, WorkflowCompletedEventArgs e)
        {
            childProcessRegress("");
            SYS_Log _log = new SYS_Log();
            _log.instanceId = e.WorkflowInstance.InstanceId;
            _log.logContent = "-";
            _log.logLevel = "消息";
            _log.logType = "WorkflowCompleted";
            insertSYSLog("", _log);

            string v = string.Format("{0},{1},{2},{3}", e.WorkflowInstance.InstanceId.ToString(), "WorkflowCompleted", System.DateTime.Now.ToString(), "-");
            outMessage(v);
        }
    }
}
